
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Networking;

public enum EConfigType
{
    ALL_TOY,
    LEVEL,
    POS,
    SCORE
}

public class ResourcesMaanger
{
    // Start is called before the first frame update
    private static ResourcesMaanger _instance;

    public static ResourcesMaanger Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new ResourcesMaanger();
            }
            return _instance;
        }
    }

    public IEnumerator LoadConfig(string configPath, EConfigType configType = EConfigType.ALL_TOY, System.Action<IConfigData> callback = null)
    {
        string path = Path.Combine(Application.streamingAssetsPath, configPath);
        string json = "";
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.WebGLPlayer)
        {
            UnityWebRequest www = UnityWebRequest.Get(path);
            yield return www.SendWebRequest();
            if (www.result == UnityWebRequest.Result.Success)
            {
                json = www.downloadHandler.text;
                LoadByType(configType, json);
            }
        }
        else
        {
            if (File.Exists(path))
            {
                json = File.ReadAllText(path);
                LoadByType(configType, json);
            }
        }

        if (!json.IsUnityNull())
        {
            var configData = LoadByType(configType, json);
            callback?.Invoke(configData);
        }
    }

    private IConfigData LoadByType(EConfigType configType, string json)
    {
        return configType switch
        {
            EConfigType.ALL_TOY => ParseAllToies(json),
            EConfigType.LEVEL => ParseLevelConfig(json),
            EConfigType.POS => ParsePonJson(json),
            EConfigType.SCORE => ParseScoreJson(json),
            _ => null,
        };
    }

    private AllToyConfig ParseAllToies(string json) {
        AllToyConfig allToies = JsonConvert.DeserializeObject<AllToyConfig>(json);
        return allToies;
    }

    private LevelConfig ParseLevelConfig(string json) {
        LevelConfig levelConfig = JsonConvert.DeserializeObject<LevelConfig>(json);
        return levelConfig;
    }

    private PosConfig ParsePonJson(string json)
    {
        PosConfig posConfigs = new PosConfig();

        var data = JsonConvert.DeserializeObject<Dictionary<string, List<float>>>(json);
        if (data.Count > 0)
        {
            posConfigs.list = data;
        }

        // if (posConfigs != null && posConfigs.list != null)
        // {
        //     if (posConfigs != null && posConfigs.list != null && posConfigs.list.Count > 0)
        //     {
        //         return posConfigs;
        //     }
        // }
        return posConfigs;
    }

    private ScoreConfig ParseScoreJson(string json) {
        ScoreConfig posConfigs = new ScoreConfig();

        var data = JsonConvert.DeserializeObject<Dictionary<string, float>>(json);
        if (data.Count > 0)
        {
            posConfigs.scoreMap = data;
        }

        return posConfigs;
    }
}
